//
//  MSHeroInfoNode.m
//  MagicSword
//
//  Created by Netease on 15/11/17.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSHeroInfoNode.h"
#import "MSProgressBar.h"
#import "MSGameManager.h"

@implementation MSHeroInfoNode {
    MSHeroInfo *_heroInfo;
    SKSpriteNode *_bodyNode;                //样子
    SKLabelNode  *_nameNode;                //名字
    SKLabelNode  *_professionNameNode;      //职业和等级
    
    SKLabelNode  *_hpNode;                  //血
    SKLabelNode  *_attackNode;              //攻击
    SKLabelNode  *_defenseNode;             //防御
    SKLabelNode  *_critNode;                //暴击
    SKLabelNode  *_doubleHitNode;           //连击
    SKLabelNode  *_vampireNode;             //吸血
    SKLabelNode  *_missValueNode;           //闪避
    
    SKSpriteNode *_selectedSprite;          //出战
    SKLabelNode  *_selectedLabel;           //出战字
    
    MSProgressBar *_exProgressBar;          //经验条
}

- (id)init {
    self = [super initWithColor:UIColorFromRGBA(0x313147, 0.66) size:CGSizeMake(ScreenWidth/4, ScreenHeight)];
    if (self) {
        self.zPosition = 2;
        [self initSubNodes];
    }
    return self;
}

- (void)initSubNodes {
    float heroSize = 100;
    float bodyTop = ScreenHeight/2-90;
    _bodyNode = [SKSpriteNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(heroSize, heroSize)];
    _bodyNode.position = CGPointMake(-CGRectGetWidth(self.frame)/4+5, bodyTop);
    _bodyNode.zPosition = 3;
    [self addChild:_bodyNode];
    
    _nameNode = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
    _nameNode.fontSize = 17;
    _nameNode.position = CGPointMake(CGRectGetWidth(self.frame)/4-5, bodyTop-3);
    _nameNode.zPosition = 3;
    [self addChild:_nameNode];
    
    _professionNameNode = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
    _professionNameNode.fontSize = 12;
    _professionNameNode.fontColor = UIColorFromRGB(0xe7e7e7);
    _professionNameNode.position = CGPointMake(CGRectGetWidth(self.frame)/4-5, bodyTop-20);
    _professionNameNode.zPosition = 3;
    [self addChild:_professionNameNode];
    
    _exProgressBar = [[MSProgressBar alloc] initWithSize:CGSizeMake(CGRectGetWidth(self.frame)-20, 20)];
    _exProgressBar.position = CGPointMake(0, bodyTop-48);
    [self addChild:_exProgressBar];
    
    
    float labelBottom = bodyTop-50;
    float labelLeft = -65;
    _hpNode = [self createInfoLableNode:CGPointMake(labelLeft, labelBottom-30)];
    [self addChild:_hpNode];
    _attackNode = [self createInfoLableNode:CGPointMake(labelLeft, labelBottom-50)];
    [self addChild:_attackNode];
//    _defenseNode = [self createInfoLableNode:CGPointMake(labelLeft, labelBottom-70)];
//    [self addChild:_defenseNode];
//    _critNode = [self createInfoLableNode:CGPointMake(labelLeft, labelBottom-70)];
//    [self addChild:_critNode];
    _doubleHitNode = [self createInfoLableNode:CGPointMake(labelLeft, labelBottom-70)];
    [self addChild:_doubleHitNode];
    _vampireNode = [self createInfoLableNode:CGPointMake(labelLeft, labelBottom-90)];
    [self addChild:_vampireNode];
    _missValueNode = [self createInfoLableNode:CGPointMake(labelLeft, labelBottom-110)];
    [self addChild:_missValueNode];
    
    _selectedSprite = [SKSpriteNode spriteNodeWithImageNamed:@"main_button"];
    _selectedSprite.zPosition = 3;
    _selectedSprite.name = NodeName_HeroFigintSeletedNodes;
    _selectedSprite.position = CGPointMake(0, -ScreenHeight/2+30);
    [self addChild:_selectedSprite];
    
    _selectedLabel = [self createInfoLableNode:CGPointMake(0, -10)];
    _selectedLabel.zPosition = 4;
    [_selectedLabel setText:@"出 战"];
    _selectedLabel.name = NodeName_HeroFigintSeletedNodes;
    _selectedLabel.fontColor = [UIColor whiteColor];
    _selectedLabel.fontSize = 14;
    _selectedLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
    [_selectedSprite addChild:_selectedLabel];
}

- (void)setDoneButtonHidden:(BOOL)hidden {
    _selectedSprite.hidden = hidden;
}

- (void)setHeroInfo:(MSHeroInfo *)heroInfo {
    [_bodyNode setTexture:heroInfo.bodyTexture];
    [_nameNode setText:heroInfo.name];
    NSString *pl = [NSString stringWithFormat:@"%@ lv.%d", heroInfo.professionName, heroInfo.level];
    [_professionNameNode setText:pl];
    [_hpNode setText:[NSString stringWithFormat:@"血量：%d", heroInfo.MaxHP]];
    [_attackNode setText:[NSString stringWithFormat:@"攻击：%d", heroInfo.Attack]];
//    [_defenseNode setText:[NSString stringWithFormat:@"防御：%d", heroInfo.Defense]];
//    [_critNode setText:[NSString stringWithFormat:@"暴击：%d", heroInfo.Crit]];
    [_doubleHitNode setText:[NSString stringWithFormat:@"连击：+%d%%", heroInfo.DoubleHit]];
    [_vampireNode setText:[NSString stringWithFormat:@"吸血：+%d%%", heroInfo.Vampire]];
    [_missValueNode setText:[NSString stringWithFormat:@"闪避：+%d%%", heroInfo.MissValue]];
    
    NSInteger nextExp = [[MSGameManager shareManager] getLevelNeedExperience:heroInfo.level];
    [_exProgressBar.titleLabelNode setText:[NSString stringWithFormat:@"%d/%d", heroInfo.Experience, nextExp]];
    [_exProgressBar setProgress:heroInfo.Experience/(float)nextExp isBlood:NO];
    
    if (heroInfo.isInFight) {
        [_selectedLabel setText:@"取消出战"];
    } else {
        [_selectedLabel setText:@"出 战"];
    }
}

- (void)setProgressBar:(NSInteger)n m:(NSInteger)m isBlood:(BOOL)isBlood {
    [_exProgressBar.titleLabelNode setText:[NSString stringWithFormat:@"%d/%d", n, m]];
    [_exProgressBar setProgress:n/(float)m isBlood:isBlood];
}

- (SKLabelNode *)createInfoLableNode:(CGPoint)point {
    SKLabelNode *labelNode = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
    labelNode.fontSize = 12;
    labelNode.fontColor = UIColorFromRGB(0xe7e7e7);
    labelNode.position = point;
    labelNode.zPosition = 3;
    labelNode.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
    return labelNode;
}

@end
